package com.teach_sys.serlvet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.teach_sys.bean.Student;
import com.teach_sys.service.StudentService;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "StudentServlet", value = "/StudentServlet")
public class StudentServlet extends HttpServlet {

    private final StudentService studentService = new StudentService();

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (request.getParameter("action").equals("queryAllStudent")) {
            queryAllStudent(response);
        } else if (request.getParameter("action").equals("queryStudentBySno")) {
            queryStudentBySno(request, response);
        } else if (request.getParameter("action").equals("queryStudentBySname")) {
            queryStudentBySname(request, response);
        } else if (request.getParameter("action").equals("addStudent")) {
            addStudent(request, response);
        } else if (request.getParameter("action").equals("deleteStudent")) {
            deleteStudent(request, response);
        } else if (request.getParameter("action").equals("updateStudent")) {
            updateStudent(request, response);
        }
    }

    public void queryAllStudent(HttpServletResponse response) throws IOException {
        List<Student> studentList = studentService.queryAllStudent();
        String result = JSON.toJSONString(studentList);
        PrintWriter printWriter = response.getWriter();
        printWriter.write(result);
    }

    public void queryStudentBySno(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String sno = request.getParameter("Sno");
        List<Student> studentList = studentService.queryStudentBySno(sno);
        String result = JSON.toJSONString(studentList);
        PrintWriter printWriter = response.getWriter();
        printWriter.write(result);
    }

    public void queryStudentBySname(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String sname = request.getParameter("Sname");
        List<Student> studentList = studentService.queryStudentBySname(sname);
        String result = JSON.toJSONString(studentList);
        PrintWriter printWriter = response.getWriter();
        printWriter.write(result);
    }

    public void addStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Student student = JSON.parseObject(request.getParameter("formObject"), Student.class);
        studentService.addStudent(student);
    }

    public void deleteStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String sno = request.getParameter("Sno");
        JSONArray jsonArray = JSON.parseArray(sno);
        List<String> deleteList = jsonArray.toJavaList(String.class);
        for (String i : deleteList) {
            studentService.deleteStudentBySno(i);
        }
    }

    public void updateStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Student student = JSON.parseObject(request.getParameter("formObject"), Student.class);
        studentService.updateStudent(student);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    }

    public void destroy() {
    }
}

